package cn.oveay.classdesign.controller.servlet;

import cn.oveay.classdesign.bean.basebean.Option;
import cn.oveay.classdesign.bean.basebean.Subject;
import cn.oveay.classdesign.bean.basebean.User;
import cn.oveay.classdesign.service.SubjectService;
import cn.oveay.classdesign.service.impl.SubjectServiceImpl;
import cn.oveay.classdesign.util.commons.DateFormatter;
import cn.oveay.classdesign.util.commons.Nanoflake;
import cn.oveay.classdesign.util.commons.exception.DateFormatException;
import cn.oveay.classdesign.util.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/list/add")
public class AddServlet extends BaseServlet {
    private SubjectService subjectService = new SubjectServiceImpl();

    public String forward(HttpServletRequest req, HttpServletResponse resp){
        return "/WEB-INF/pages/add.jsp";
    }

    public String update(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        String voteType = req.getParameter("voteType");
        String title = req.getParameter("title");
        Long endTime = null;
        Long startTime = null;
        if(title.trim().equals("")){
            req.setAttribute("error", "必须填写标题.");
            req.setAttribute("type", voteType);
            return "f:/WEB-INF/pages/add.jsp";
        }
        List<String> list = new ArrayList<>();
        String[] temp = req.getParameterValues("options");
        for(String t : temp){
            if(!t.trim().equals("")){
                list.add(t);
            }
        }
        if(list.size() < 2) {
            req.setAttribute("error", "选项至少为两个.");
            if(list.size() == 1) {
                req.setAttribute("option", list.get(0));
            }
            req.setAttribute("title", title);
            req.setAttribute("type", voteType);
            return "f:/WEB-INF/pages/add.jsp";
        }
        try {
            endTime = DateFormatter.formatToTimestamp(req.getParameter("endDate").replace('T', ' ') + ":00");
        } catch (DateFormatException e) {
            req.setAttribute("title", title);
            req.setAttribute("type", voteType);
            req.setAttribute("error", "结束时间必须填写.");
            return "f:/WEB-INF/pages/add.jsp";
        }
        try {
            startTime = DateFormatter.formatToTimestamp(req.getParameter("startDate").replace('T', ' ') + ":00");
        } catch (DateFormatException e) {
            req.setAttribute("title", title);
            req.setAttribute("type", voteType);
            req.setAttribute("endDate", req.getParameter("endDate"));
            req.setAttribute("error", "开始时间必须填写.");
            return "f:/WEB-INF/pages/add.jsp";
        }
        if(startTime < new Date().getTime()){
            req.setAttribute("title", title);
            req.setAttribute("type", voteType);
            req.setAttribute("endDate", req.getParameter("endDate"));
            req.setAttribute("startDate", req.getParameter("startDate"));
            req.setAttribute("error", "开始时间必须在未来.");
            return "f:/WEB-INF/pages/add.jsp";
        }
        if(endTime < startTime){
            req.setAttribute("title", title);
            req.setAttribute("type", voteType);
            req.setAttribute("error", "时间必须是开始时间未来的时间.");
            req.setAttribute("endDate", req.getParameter("endDate"));
            req.setAttribute("startDate", req.getParameter("startDate"));
            return "f:/WEB-INF/pages/add.jsp";
        }
        // 构造subject对象
        User user = (User) req.getSession().getAttribute("user");
        Subject subject = new Subject();
        subject.setTitle(title);
        subject.setNumber(Integer.valueOf(voteType));
        subject.setUser(user);
        subject.setId(Nanoflake.getNanoflake());
        subject.setUserId(user.getId());
        subject.setStarttime(startTime);
        subject.setEndtime(endTime);
        subject.setStartTimeView(new Date(subject.getStarttime()).toString());
        if(subject.getNumber() == 1){
            subject.setNumberView(Subject.SINGLE_NAME);
        }else{
            subject.setNumberView(Subject.MULTI_NAME);
        }
        List<Option> optionList = new ArrayList<>();
        for(int i = 0; i < list.size(); i++){
            Option option = new Option();
            option.setContent(list.get(i));
            option.setIdx(i + 1);
            option.setSubjectId(subject.getId());
            option.setId(Nanoflake.getNanoflake());
            optionList.add(option);
        }
        subject.setOptions(optionList);
        subjectService.inseart(subject);
        return "r:/list?method=forward";
    }

    public String find(HttpServletRequest req, HttpServletResponse resp){
        return "/WEB-INF/pages/add.jsp";
    }
}
